package com.taotao.sso.dao.ipml;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.taotao.sso.dao.IJedisClient;

public class JedisClientSingle implements IJedisClient {

	@Autowired
	private JedisPool jedisPool;

	@Override
	public String get(String key) {
		Jedis jedis = null;
		String str = null;
		try {
			jedis = jedisPool.getResource();
			str = jedis.get(key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return str;
	}

	@Override
	public String set(String key, String value) {
		String str = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			str = jedis.set(key, value);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return str;
	}

	@Override
	public String hget(String hkey, String key) {
		Jedis jedis = null;
		String str = null;
		try {
			jedis = jedisPool.getResource();
			str = jedis.hget(hkey, key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (jedis != null)
				jedis.close();
		}
		return str;
	}

	@Override
	public long hset(String hkey, String key, String value) {
		Jedis jedis = null;
		Long l = null;
		try {
			jedis = jedisPool.getResource();
			l = jedis.hset(hkey, key, value);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (jedis != null)
				jedis.close();
		}
		return l;
	}

	@Override
	public long incr(String key) {
		Long l = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			l = jedis.incr(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return l;
	}

	@Override
	public long expire(String key, int second) {
		Long l = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			l = jedis.expire(key, second);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return l;
	}

	@Override
	public long ttl(String key) {
		Jedis jedis = null;
		Long l = null;
		try {
			jedis = jedisPool.getResource();
			l = jedis.ttl(key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return l;
	}

	@Override
	public long hdel(String hkey, String key) {
		// TODO Auto-generated method stub
		Jedis jedis = null;
		Long l = null;
		try {
			jedis = jedisPool.getResource();
			l = jedis.hdel(hkey, key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return l;
	}

	@Override
	public long del(String key) {
		Jedis jedis = null;
		Long l = null;
		try {
			jedis = jedisPool.getResource();
			l = jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return l;
	}

}
